/*
 * ﻿Copyright (C) 2009-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.gwetons.logging.shared;

import junit.framework.TestCase;

import com.newmainsoftech.gwetons.logging.client.LoggerInitializerForTestGinjector;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrap;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrapFactoryInstanceHolder;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrapFactory;

public abstract class LoggerWrapAdaptedTestCase extends TestCase {
	// For logging --------------------------------------------------------------------------------
	abstract protected String getNameForLogging();
	
	private static LoggerInitializerForTest loggerInitializerForTest;
		protected static LoggerInitializerForTest getLoggerInitializerForTest() {
			return loggerInitializerForTest;
		}
		protected static void setLoggerInitializerForTest( 
				LoggerInitializerForTest loggerInitializerForTest) {
			
			LoggerWrapAdaptedTestCase.loggerInitializerForTest = loggerInitializerForTest;
			loggerInitializerForTest.setupLogger();
		}

	private LoggerWrapFactory loggerWrapFactory;
		/**
		 * @return {@link #loggerWrapFactory} member field. <br />
		 * If <code>loggerWrapFactory</code> member field value is null when this method is called, then 
		 * it will be obtained via {@link LoggerInitializerForTestGinjector}. 
		 */
		protected LoggerWrapFactory getLoggerWrapFactory() {
			LoggerInitializerForTest loggerInitializerForTest
			= LoggerWrapAdaptedTestCase.getLoggerInitializerForTest();
				if ( loggerInitializerForTest == null) {
					loggerInitializerForTest 
					= LoggerInitializerForTestAccessor.getLoggerInitializerForTestInstance();
						LoggerWrapAdaptedTestCase.setLoggerInitializerForTest( loggerInitializerForTest);
						loggerInitializerForTest.setupLogger();
				}
			
			if ( loggerWrapFactory == null) {
				setLoggerWrapFactory( 
						LoggerWrapFactoryInstanceHolder.getLoggerWrapFactory()
						);
			}
			
			return loggerWrapFactory;
		}
		protected void setLoggerWrapFactory( LoggerWrapFactory loggerWrapFactory) {			
			this.loggerWrapFactory = loggerWrapFactory;
			setLoggerWrap( null);
		}

	private LoggerWrap loggerWrap;
		/**
		 * @return {@link #loggerWrap} member field. <br />
		 * If <code>loggerWrap</code> member field value is null when this method is called, 
		 * then it will be obtained via {@link LoggerWrapFactory#getLoggerWrap(String)} method of 
		 * {@link #loggerWrapFactory} member field. At that time, if <code>loggerWrapFactory</code> 
		 * member field value is null, then {@link LoggerWrapFactory} instance from 
		 * {@link LoggerWrapFactoryInstanceHolder#getLoggerWrapFactory()} method will be set to 
		 * <code>loggerWrapFactory</code> member field. 
		 */
		protected LoggerWrap getLoggerWrap() {
			if ( loggerWrap == null) {
				setLoggerWrap( 
						getLoggerWrapFactory().getLoggerWrap( getNameForLogging())
						);
			}
			return loggerWrap;
		}
		protected void setLoggerWrap( LoggerWrap loggerWrap) {
			this.loggerWrap = loggerWrap;
		}
	// --------------------------------------------------------------------------------------------
}
